/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import wads.drawing.node.GroupNode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

public class GroupNodeDialog extends BaseWADDialog {
//  private static final long serialVersionUID = 1191740193930176867L;
  
  private JLabel DialogLabel = new JLabel();
  private JCheckBox SelectAllCheckBox = new JCheckBox("Select All");
  private JCheckBox[] UserApprovedCheckBox;
  private GroupNode wg_owner;
  private JLabel no_nodes_inside_icon = new JLabel(new ImageIcon("images/no_user_image.jpg")); //image size = 52x64 pixels
  
  private static final String NO_USER_DEFINED_TEXT = "User not defined";
  private static final String NO_NODES_INSIDE_TEXT = "No User(s) inside.";
  private static final String DEFAULT_DIALOG_LABEL_TEXT = "Set Approved User";
  private static final Font NO_USER_DEFINED_FONT = new Font("Tahoma", Font.ITALIC, 13);
  private static final Font DEFAULT_USER_FONT = new Font("Tahoma", Font.PLAIN, 13);
  private static final Font DEFAULT_LABEL_FONT = new Font("Dialog", Font.BOLD, 12);
  private static final Font NO_NODES_INSIDE_FONT = new Font("Dialog", Font.BOLD, 12);

  public GroupNodeDialog(Frame parent, GroupNode owner){
    super(parent, owner);
    this.setTitle("Set User for each Node in Group");
    
    wg_owner = owner;
    
    UserApprovedCheckBox = new JCheckBox[owner.countNodesInside()];
    
    try{
      jbInit();
    }
    catch(Exception e){ e.printStackTrace(); }
  }
  
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    DialogLabel.setFont(DEFAULT_LABEL_FONT);
    DialogLabel.setForeground(Color.BLACK);
    DialogLabel.setText(DEFAULT_DIALOG_LABEL_TEXT);
    DialogLabel.setBounds(new Rectangle(30, 10, 120, 20));
    
    no_nodes_inside_icon.setBounds(new Rectangle(149, 40, 60, 70));
    no_nodes_inside_icon.setVisible(false);
    
    SelectAllCheckBox.setBounds(new Rectangle(40, 40, 100, 20));
    SelectAllCheckBox.addActionListener(new ActionListener(){
            @Override
      public void actionPerformed(ActionEvent e){
        SelectAllCheckBox_actionPerformed(e);
      }
    });
    
    for(int i=0;i<wg_owner.countNodesInside();i++){
      UserApprovedCheckBox[i] = new JCheckBox();
      UserApprovedCheckBox[i].setSelected(true/*wg_owner.getNodes().elementAt(i).getNodeApprovedStatus()*/);
      UserApprovedCheckBox[i].setBounds(new Rectangle(45, 65+(i*25), 200, 20));
      
      if(wg_owner.getNodes().elementAt(i).getName() == null){
        UserApprovedCheckBox[i].setForeground(Color.RED);
        UserApprovedCheckBox[i].setFont(NO_USER_DEFINED_FONT);
        UserApprovedCheckBox[i].setText(NO_USER_DEFINED_TEXT);
      }
      else{
        UserApprovedCheckBox[i].setForeground(Color.BLACK);
        UserApprovedCheckBox[i].setFont(DEFAULT_USER_FONT);
        UserApprovedCheckBox[i].setText(wg_owner.getNodes().elementAt(i).getName());
      }
      
      UserApprovedCheckBox[i].addItemListener(new ItemListener(){
                @Override
        public void itemStateChanged(ItemEvent e){
          UserApprovedCheckBox_itemStateChanged(e);
        }
      });
      this.getContentPane().add(UserApprovedCheckBox[i], null);
    }
    
    if(wg_owner.countNodesInside() < 1){
      no_nodes_inside_icon.setVisible(true);
      SelectAllCheckBox.setVisible(false);
      
      DialogLabel.setText(NO_NODES_INSIDE_TEXT);
      DialogLabel.setFont(NO_NODES_INSIDE_FONT);
      DialogLabel.setForeground(Color.RED);
    }
    else
      SelectAllCheckBox.setSelected(isAllUserCheckBoxesSelected());
    
    this.getContentPane().add(no_nodes_inside_icon, null);
    this.getContentPane().add(DialogLabel, null);
    this.getContentPane().add(SelectAllCheckBox, null);
  }
  
    @Override
  public void initDisplay(){
    
  }
  
    @Override
  public void saveProperties(){
    
  }
  
  private void SelectAllCheckBox_actionPerformed(ActionEvent ie){
    if(SelectAllCheckBox.isSelected() && wg_owner.countNodesInside() > 0){
      for(int i=0;i<wg_owner.countNodesInside();i++){
        UserApprovedCheckBox[i].setSelected(true);
      }
    }
    else if(!SelectAllCheckBox.isSelected() && wg_owner.countNodesInside() > 0){
      for(int i=0;i<wg_owner.countNodesInside();i++){
        UserApprovedCheckBox[i].setSelected(false);
      }
    }
    else
      return;
  }
  
  private void UserApprovedCheckBox_itemStateChanged(ItemEvent ie){
    if(ie.getStateChange() == ItemEvent.SELECTED){
      if(isAllUserCheckBoxesSelected()){
        SelectAllCheckBox.setSelected(true);
      }
      else
        SelectAllCheckBox.setSelected(false);
    }
    else if(ie.getStateChange() == ItemEvent.DESELECTED){
      SelectAllCheckBox.setSelected(false);
    }
    else
      return;
  }
  
  private boolean isAllUserCheckBoxesSelected(){
    for(int i=0;i<wg_owner.countNodesInside();i++){
      if(!UserApprovedCheckBox[i].isSelected())
        return false;
    }
    
    return true;
  }
}
